package collect.map;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

public class Mapbianli1 {
    public static void main(String[] args) {
        //创建map对象
        Map<String, String> map = new HashMap<>();
        //添加元素
        map.put("杨过", "小龙女");
        map.put("杨", "小龙女");
        map.put("过", "小龙女");
        System.out.println(map);
        //增强for
        //遍历集合，通过键值对
        Set<Map.Entry<String, String>> entries = map.entrySet();
        for (Map.Entry<String, String> entry : entries) {
            String key = entry.getKey();
            String value = entry.getValue();
            System.out.println(key + "=" + value);
        }
        //通过键找值
        Set<String> strings = map.keySet();
        for (String string : strings) {
            String s = map.get(string);
            System.out.println(string + "=" + s);
        }
        System.out.println("===================");
        //迭代器
        //键值对
        Set<Map.Entry<String, String>> entries1 = map.entrySet();
        Iterator<Map.Entry<String, String>> iterator = entries1.iterator();
        while (iterator.hasNext()) {
            Map.Entry<String, String> next = iterator.next();
            String key = next.getKey();
            String value = next.getValue();
            System.out.println(key + "=" + value);
        }
        System.out.println("========================");
        //lambda遍历
        //键值对
        map.forEach((key, value) -> System.out.println(key + "=" + value));
        System.out.println("==================");
        Set<String> strings1 = map.keySet();
        strings1.forEach(s -> {
            String s1 = map.get(s);
            System.out.println(s + "=" + s1);
            return;
        });

    }
}
